package org.rency.crawler.fetch.repository.impl;

import java.util.List;
import javax.annotation.Resource;
import org.rency.common.utils.converts.JsonConvert;
import org.rency.common.utils.enums.YesNoEnum;
import org.rency.crawler.common.CrawlerConstants;
import org.rency.crawler.common.enums.FirLevelKind;
import org.rency.crawler.common.enums.SecLevelKind;
import org.rency.crawler.common.helper.ValidatorHelper;
import org.rency.crawler.dal.dao.MetaResourceDao;
import org.rency.crawler.dal.entity.MetaResourceDO;
import org.rency.crawler.domain.vo.MetaResourceDomain;
import org.rency.crawler.fetch.converter.DomainConvertor;
import org.rency.crawler.fetch.repository.MetaResourceRepository;
import org.rency.ucs.annotation.CacheInvalid;
import org.rency.ucs.annotation.CacheResult;
import org.springframework.stereotype.Repository;

/**
 * 描述: ${TODO}<br>
 * 最近修改时间:2017/11/22 10:19<br>
 *
 * @author CaiyuRen
 * @version V1.0
 * @since 2017/11/22 10:19
 */
@Repository
public class MetaResourceRepositoryImpl implements MetaResourceRepository,CrawlerConstants {

  @Resource
  private MetaResourceDao metaResourceDao;

  @Resource(name = "metaResourceDomainConvertor")
  private DomainConvertor<MetaResourceDomain, MetaResourceDO> convertor;

  @Override
  @CacheInvalid(key = "meta",allEntries = true)
  public void refreshAll() {
  }

  @Override
  @CacheResult(key = "meta:+#id",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public MetaResourceDomain load(String id) {
    return convertor.convertDomain(metaResourceDao.load(id));
  }

  @Override
  @CacheResult(key = "meta:+ #secLevelKind.parent.code + #secLevelKind.code",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public MetaResourceDomain find(SecLevelKind secLevelKind) {
    return this.find(secLevelKind.getParent(),secLevelKind);
  }

  @Override
  @CacheResult(key = "meta:+ #firLevelKind.code + #secLevelKind.code",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public MetaResourceDomain find(FirLevelKind firLevelKind, SecLevelKind secLevelKind) {
    return convertor.convertDomain(metaResourceDao.find(firLevelKind.getCode(),secLevelKind.getCode()));
  }

  @Override
  @CacheResult(key = "meta:+ #firLevelKind.code",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public List<MetaResourceDomain> find(FirLevelKind firLevelKind) {
    return convertor.convertDomain(metaResourceDao.find(firLevelKind.getCode()));
  }

  @Override
  public boolean save(MetaResourceDomain domain) {
    return metaResourceDao.save(convertor.convertEntity(domain));
  }

  @Override
  @CacheInvalid(key = "meta")
  public boolean updateEnable(String id, YesNoEnum newEnable, YesNoEnum oldEnable) {
    return metaResourceDao.updateEnable(id,newEnable,oldEnable);
  }

  @Override
  @CacheResult(key = "meta:FETCH:FETCH_INTERVAL_TIME",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public long getFetchIntervalTime() {
    MetaResourceDomain domain = this.find(SecLevelKind.FETCH_INTERVAL_TIME);
    ValidatorHelper.notNull(domain,SecLevelKind.FETCH_INTERVAL_TIME.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.FETCH_INTERVAL_TIME.getMsg());
    return Long.parseLong(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:FETCH:FETCH_RETRY_TIME",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public int getFetchRetryTimes() {
    MetaResourceDomain domain = this.find(SecLevelKind.FETCH_RETRY_TIME);
    ValidatorHelper.notNull(domain,SecLevelKind.FETCH_RETRY_TIME.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.FETCH_RETRY_TIME.getMsg());
    return Integer.parseInt(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:FETCH:ROBOTS_SWITCH",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public boolean getRobotsSwitch() {
    MetaResourceDomain domain = this.find(SecLevelKind.ROBOTS_SWITCH);
    ValidatorHelper.notNull(domain,SecLevelKind.ROBOTS_SWITCH.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.ROBOTS_SWITCH.getMsg());
    return YesNoEnum.YES == YesNoEnum.get(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:CACHE:ROBOTS_EXPIRE_TIME",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public long getRobotsPolicyExpireTime() {
    MetaResourceDomain domain = this.find(SecLevelKind.ROBOTS_EXPIRE_TIME);
    ValidatorHelper.notNull(domain,SecLevelKind.ROBOTS_EXPIRE_TIME.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.ROBOTS_EXPIRE_TIME.getMsg());
    return Long.parseLong(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:CACHE:FETCH_EXT_EXPIRE_TIME",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public long getFetchExtExpireTime() {
    MetaResourceDomain domain = this.find(SecLevelKind.FETCH_EXT_EXPIRE_TIME);
    ValidatorHelper.notNull(domain,SecLevelKind.FETCH_EXT_EXPIRE_TIME.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.FETCH_EXT_EXPIRE_TIME.getMsg());
    return Long.parseLong(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:FETCH:FETCH_DEPTH",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public int getFetchDepth() {
    MetaResourceDomain domain = this.find(SecLevelKind.FETCH_DEPTH);
    ValidatorHelper.notNull(domain,SecLevelKind.FETCH_DEPTH.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.FETCH_DEPTH.getMsg());
    return Integer.parseInt(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:FETCH:FETCH_WEIGHT",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public double getFetchWeight() {
    MetaResourceDomain domain = this.find(SecLevelKind.FETCH_WEIGHT);
    ValidatorHelper.notNull(domain,SecLevelKind.FETCH_WEIGHT.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.FETCH_WEIGHT.getMsg());
    return Double.parseDouble(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:CACHE:FETCHED_EXPIRE_TIME",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public long getFetchedQueueExpireTime() {
    MetaResourceDomain domain = this.find(SecLevelKind.FETCHED_EXPIRE_TIME);
    ValidatorHelper.notNull(domain,SecLevelKind.FETCHED_EXPIRE_TIME.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.FETCHED_EXPIRE_TIME.getMsg());
    return Long.parseLong(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:CACHE:HOST_EXPIRE_TIME",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public long getHostExpireTime() {
    MetaResourceDomain domain = this.find(SecLevelKind.HOST_EXPIRE_TIME);
    ValidatorHelper.notNull(domain,SecLevelKind.HOST_EXPIRE_TIME.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.HOST_EXPIRE_TIME.getMsg());
    return Long.parseLong(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:INET:HTTP_CONNECTION_TIMEOUT",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public int getHttpConnectTimeout() {
    MetaResourceDomain domain = this.find(SecLevelKind.HTTP_CONNECTION_TIMEOUT);
    ValidatorHelper.notNull(domain,SecLevelKind.HTTP_CONNECTION_TIMEOUT.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.HTTP_CONNECTION_TIMEOUT.getMsg());
    return Integer.parseInt(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:INET:HTTP_SOCKET_TIMEOUT",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public int getHttpConnectSocketTimeout() {
    MetaResourceDomain domain = this.find(SecLevelKind.HTTP_SOCKET_TIMEOUT);
    ValidatorHelper.notNull(domain,SecLevelKind.HTTP_SOCKET_TIMEOUT.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.HTTP_SOCKET_TIMEOUT.getMsg());
    return Integer.parseInt(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:INET:HTTP_POOL_MAX_SIZE",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public int getHttpConnectMaxTotal() {
    MetaResourceDomain domain = this.find(SecLevelKind.HTTP_POOL_MAX_SIZE);
    ValidatorHelper.notNull(domain,SecLevelKind.HTTP_POOL_MAX_SIZE.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.HTTP_POOL_MAX_SIZE.getMsg());
    return Integer.parseInt(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:INET:HTTP_RETRY_TIMES",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public int getHttpRetryTimes() {
    MetaResourceDomain domain = this.find(SecLevelKind.HTTP_RETRY_TIMES);
    ValidatorHelper.notNull(domain,SecLevelKind.HTTP_RETRY_TIMES.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.HTTP_RETRY_TIMES.getMsg());
    return Integer.parseInt(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:INET:HTTP_ALLOW_REDIRECT",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public boolean getHttpAllowRedirect() {
    MetaResourceDomain domain = this.find(SecLevelKind.HTTP_ALLOW_REDIRECT);
    ValidatorHelper.notNull(domain,SecLevelKind.HTTP_ALLOW_REDIRECT.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.HTTP_ALLOW_REDIRECT.getMsg());
    return YesNoEnum.YES == YesNoEnum.get(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:INET:HTTP_PROXY_SWITCH",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public boolean getHttpProxySwitch() {
    MetaResourceDomain domain = this.find(SecLevelKind.HTTP_PROXY_SWITCH);
    ValidatorHelper.notNull(domain,SecLevelKind.HTTP_PROXY_SWITCH.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.HTTP_PROXY_SWITCH.getMsg());
    return YesNoEnum.YES == YesNoEnum.get(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:INET:HTTP_BROWSER_AGENTS",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public List<String> getHttpBrowserAgents() {
    MetaResourceDomain domain = this.find(SecLevelKind.HTTP_BROWSER_AGENTS);
    ValidatorHelper.notNull(domain,SecLevelKind.HTTP_BROWSER_AGENTS.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.HTTP_BROWSER_AGENTS.getMsg());
    return JsonConvert.toObject(domain.getSecLevelValue(),List.class);
  }

  @Override
  @CacheResult(key = "meta:FETCH:FETCH_SNAPSHOT",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public boolean generateSnapshot() {
    MetaResourceDomain domain = this.find(SecLevelKind.FETCH_SNAPSHOT);
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.FETCH_SNAPSHOT.getMsg());
    return YesNoEnum.YES == YesNoEnum.get(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:FETCH:FETCH_IMG",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public boolean fetchImgSwitch() {
    MetaResourceDomain domain = this.find(SecLevelKind.FETCH_IMG);
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.FETCH_IMG.getMsg());
    return YesNoEnum.YES == YesNoEnum.get(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:FETCH:FETCH_SCRIPT",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public boolean fetchScriptSwitch() {
    MetaResourceDomain domain = this.find(SecLevelKind.FETCH_SCRIPT);
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.FETCH_SCRIPT.getMsg());
    return YesNoEnum.YES == YesNoEnum.get(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:FETCH:FETCH_STYLE",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public boolean fetchStyleSwitch() {
    MetaResourceDomain domain = this.find(SecLevelKind.FETCH_STYLE);
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.FETCH_STYLE.getMsg());
    return YesNoEnum.YES == YesNoEnum.get(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:FETCH:FETCH_SAME_SITE",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public boolean fetchSameSite() {
    MetaResourceDomain domain = this.find(SecLevelKind.FETCH_SAME_SITE);
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.FETCH_SAME_SITE.getMsg());
    return YesNoEnum.YES == YesNoEnum.get(domain.getSecLevelValue());
  }

  @Override
  @CacheResult(key = "meta:FETCH:FETCH_EXCLUDE_URI_PREFIX",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public List<String> getFetchExcludeUriPrefix() {
    MetaResourceDomain domain = this.find(SecLevelKind.FETCH_EXCLUDE_URI_PREFIX);
    ValidatorHelper.notNull(domain,SecLevelKind.FETCH_EXCLUDE_URI_PREFIX.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.FETCH_EXCLUDE_URI_PREFIX.getMsg());
    return JsonConvert.toObject(domain.getSecLevelValue(),List.class);
  }

  @Override
  @CacheResult(key = "meta:FETCH:FETCH_EXCLUDE_URI_SUFFIX",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public List<String> getFetchExcludeUriSuffix() {
    MetaResourceDomain domain = this.find(SecLevelKind.FETCH_EXCLUDE_URI_SUFFIX);
    ValidatorHelper.notNull(domain,SecLevelKind.FETCH_EXCLUDE_URI_SUFFIX.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.FETCH_EXCLUDE_URI_SUFFIX.getMsg());
    return JsonConvert.toObject(domain.getSecLevelValue(),List.class);
  }

  @Override
  @CacheResult(key = "meta:INET:HTTP_FILE_CONTENT_TYPE",expireSecond = DEFAULT_CACHE_EXPIRE_TIME_SEC)
  public List<String> getHttpContentTypeWithFile() {
    MetaResourceDomain domain = this.find(SecLevelKind.HTTP_FILE_CONTENT_TYPE);
    ValidatorHelper.notNull(domain,SecLevelKind.HTTP_FILE_CONTENT_TYPE.getMsg());
    ValidatorHelper.notEmpty(domain.getSecLevelValue(),SecLevelKind.HTTP_FILE_CONTENT_TYPE.getMsg());
    return JsonConvert.toObject(domain.getSecLevelValue(),List.class);
  }
}
